package com.gzyj.flink.gps.sink.gp;

import com.gzyj.flink.gps.factory.MybatisSessionFactory;
import com.gzyj.flink.gps.mapper.tidb.TsRiskAreaAlarmMapper;
import com.gzyj.flink.pojo.gps.TsRiskAreaAlarm;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class AreaRiskAlarmSink extends RichSinkFunction<TsRiskAreaAlarm> {
    SqlSessionFactory sqlSessionFactory;

    @Override
    public void open(Configuration parameters) throws Exception {
        sqlSessionFactory = MybatisSessionFactory.getSqlSessionFactory();
    }

    @Override
    public void invoke(TsRiskAreaAlarm value, Context context) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        try {
            TsRiskAreaAlarmMapper tsRiskAreaAlarmMapper = sqlSession.getMapper(TsRiskAreaAlarmMapper.class);

            tsRiskAreaAlarmMapper.insertSelective(value);


            sqlSession.commit();

        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
    }
}
